package depots

import (
	"encoding/json"
	"github.com/astaxie/beego/logs"
)

// 天猫精灵意图
type Aligenie struct {
	SessionId    string                 `form:"sessionId"` // 会话ID，session内的对话此ID相同
	BotId        int64                  `form:"botId"`     // 应用ID，来自于创建的应用或者技能
	Utterance    string                 `form:"utterance"` // 用户输入语句
	SkillId      int64                  `form:"skillId"`
	SkillName    string                 `form:"skillName"`
	Token        string                 `form:"token"`
	DomainId     int64                  `form:"domainId"`
	RequestId    string                 `form:"requestId"`
	IntentId     int64                  `form:"intentId"`
	IntentName   string                 `form:"intentName"`
	RequestData  map[string]string      `form:"requestData"`
	SlotEntities []Aligenie_SlotEntitie `form:"slotEntities"`
}

type Aligenie_SlotEntitie struct {
	IntentParameterId   int64  `form:"intentParameterId"`
	IntentParameterName string `form:"intentParameterName"`
	OriginalValue       string `form:"originalValue"`
	StandardValue       string `form:"standardValue"`
	LiveTime            int64  `form:"liveTime"`
	CreateTimeStamp     int64  `form:"createTimeStamp"`
}

// 天猫精灵技能回复内容
type Aligenie_Response struct {
	ReturnCode          string               `json:"returnCode"`
	ReturnErrorSolution string               `json:"returnErrorSolution"`
	ReturnMessage       string               `json:"returnMessage"`
	ReturnValue         Aligenie_ReturnValue `json:"returnValue"`
}

type Aligenie_ReturnValue struct {
	Reply       string               `json:"reply"`
	ResultType  string               `json:"resultType"`
	Properties  map[string]string    `json:"properties"`
	AskedInfos  []Aligenie_AskedInfo `json:"askedInfos"`
	Actions     []Aligenie_Action    `json:"actions"`
	ExecuteCode string               `json:"executeCode"`
}

type Aligenie_AskedInfo struct {
	ParameterName string `json:"parameterName"`
	IntentId      int64  `json:"intentId"`
}

type Aligenie_Action struct {
	Name       string                 `json:"name"`
	Properties map[string]interface{} `json:"properties"`
}

// 获取槽位信息
func (intent *Aligenie) GetSlotInfo(slotName string) *Aligenie_SlotEntitie {
	for _, v := range intent.SlotEntities {
		if v.IntentParameterName == slotName {
			return &v
		}
	}
	return nil
}

//"cycle": false,
//"description": "5点半",
//"fuzzyYear": false,
//"iDateString": "05:30:00",
//"solarDate": 1551196800000,
//"solarDateFormat": "2019-02-27",
//"startDate": "2019-02-27",
//"startTime": "05:30:00",
//"stats": "SUCCESS",
//"timeZone": "GMT+8:00",
//"type": "TIME"
type Aligenie_SlotEntitieTime struct {
	Cycle           bool   `json:"cycle"`
	Description     string `json:"description"`
	FuzzyYear       bool   `json:"fuzzyYear"`
	IDateString     string `json:"iDateString"`
	SolarDate       int64  `json:"solarDate"`
	SolarDateFormat string `json:"solarDateFormat"`
	StartDate       string `json:"startDate"`
	StartTime       string `json:"startTime"`
	Stats           string `json:"stats"`
	TimeZone        string `json:"timeZone"`
	Type            string `json:"type"`
}

func (slot *Aligenie_SlotEntitie) ParsingTime() *Aligenie_SlotEntitieTime {
	if slot == nil {
		return nil
	}

	pt := new(Aligenie_SlotEntitieTime)
	if err := json.Unmarshal([]byte(slot.StandardValue), pt); err != nil {
		logs.Error("ParsingTime error:", err)
		return nil
	}
	return pt
}

// 文字播报
func (intent *Aligenie) ToText(msg string) *Aligenie_Response {
	res := &Aligenie_Response{
		ReturnCode: "0",
		ReturnValue: Aligenie_ReturnValue{
			Reply:       msg,
			ResultType:  "RESULT",
			ExecuteCode: "SUCCESS",
		},
	}
	return res
}

// 语音播报
func (intent *Aligenie) ToAudio(audioGenieId string) *Aligenie_Response {
	actions := []Aligenie_Action{
		Aligenie_Action{
			Name: "audioPlayGenieSource",
			Properties: map[string]interface{}{
				"audioGenieId": audioGenieId,
			},
		},
	}

	res := &Aligenie_Response{
		ReturnCode: "0",
		ReturnValue: Aligenie_ReturnValue{
			ResultType:  "RESULT",
			ExecuteCode: "SUCCESS",
			Actions:     actions,
		},
	}

	return res
}

// @Title 询问槽位
// @param parameterName string true 询问的参数名(非实体名)
// @param intentId int64 true 意图ID
func (res *Aligenie_Response) AskedInfo(parameterName string, intentId int64) *Aligenie_Response {
	askedInfos := []Aligenie_AskedInfo{
		Aligenie_AskedInfo{
			ParameterName: parameterName,
			IntentId:      intentId,
		},
	}
	res.ReturnValue.AskedInfos = askedInfos
	res.ReturnValue.ResultType = "ASK_INF"
	return res
}
